#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2020/11/25 13:26

@author: aimee.mei
"""

import logging,traceback
from util.db_util import execute_query

def get_random_risk_category_code():

    """ 随机获取一个预警分类code

    :return: 一个预警分类code
    """

    results = execute_query(
        "SELECT * FROM risk_category where status = 1 order by rand() limit 1" )
    try:
        result = results[0][2]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


def get_report_category_list(type):
    """获取所有的预警分类

    :param type: 查询的字段

    :return: name or code list
    """

    results = execute_query(
        "SELECT * FROM risk_category where status =1 " % ( ))
    try:
        if type == "name":
            re_list = [row[1] for row in results]
        else:
            re_list = [row[2] for row in results]
        return ','.join([str(n) for n in re_list])
    except Exception as e:
        logging.error("get_category_list: %s"%e)
        return ""


def get_report_events_list(type):
    """获取所有的预警事件

    :param type: 查询的字段

    :return: name or code or grade list
    """

    results = execute_query(
        "SELECT * FROM risk_event_def where status =1 " % ( ))
    try:
        if type == "event_code":
            result = [row[1] for row in results]
        elif type == "events_name":
            result = [row[2] for row in results]
        elif type == "event_grade":
            result = [row[4] for row in results]
        else:
            result = [row[0] for row in results]
        re_list = ','.join([str(n) for n in result])
#        re_list = ','.join([str(n) for n in result])
        return re_list
    except Exception as e:
        logging.error("get_events_list: %s"%e)
        return ""